/**
 * Created by Administrator on 2016/2/29.
 */
gpNamespace.JdcLayer = cc.Layer.extend({
	background:null,
	
	levelMenu:null,
	bxLayer:null,
	
	classicField:null,
	boxField:null,
	
	startItem:null,//快速开始按钮动画
	ctor: function () {
		this._super();

		this.init();
	},

	init: function () {
		//加载资源
		cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkjdcRes.PdkJdc_plist);
		ccs.armatureDataManager.addArmatureFileInfo(
				gpNamespace.jdcStartAnimationRes.JdcStart_png,
				gpNamespace.jdcStartAnimationRes.JdcStart_plist,
				gpNamespace.jdcStartAnimationRes.JdcStart_json);

		//背景图层
		var background = new cc.Sprite(gpNamespace.jdc.Jdc_Background);
		background.attr({
			x: 62,
			y: 55,
			anchorX: 0,
			anchorY: 0
		});
		this.addChild(background);
		this.background = background;

		//经典场按钮，包厢按钮
//		var classicField = new cc.MenuItemImage(gpNamespace.jdc.Jdc_ClassicFieldBt, gpNamespace.jdc.Jdc_ClassicFieldBt_down, this.jdcCallbackFunction, this);

		var classicFieldOn = new cc.MenuItemImage(gpNamespace.jdc.Jdc_ClassicFieldBt);
		var classicFieldOff = new cc.MenuItemImage(gpNamespace.jdc.Jdc_ClassicFieldBt_down);
		var classicField = new cc.MenuItemToggle(classicFieldOn, classicFieldOff, this.jdcCallbackFunction, this);
		this.classicField=classicField;
		classicField.setUserData(this);

		var boxFieldOn = new cc.MenuItemImage(gpNamespace.jdc.Jdc_Boxbt);
		var boxFieldOff = new cc.MenuItemImage(gpNamespace.jdc.Jdc_Boxbt_down);
		var boxField = new cc.MenuItemToggle(boxFieldOn, boxFieldOff, this.bxCallbackFunction, this);
		this.boxField=boxField;
		boxField.setSelectedIndex(1);
		boxField.setUserData(this);
//		var boxField = new cc.MenuItemImage(gpNamespace.jdc.Jdc_Boxbt, gpNamespace.jdc.Jdc_Boxbt_down, this.bxCallbackFunction, this);

		var totalMenu = new cc.Menu(classicField, boxField);
		totalMenu.attr({
			x: 85 + 562 - 6,
			y: 538 + 116 / 2 - 10

		});
		totalMenu.alignItemsHorizontally();
		this.addChild(totalMenu);


		//开始按钮
		var startItem = new cc.MenuItemImage(gpNamespace.jdc.Jdc_Start, gpNamespace.jdc.Jdc_Start, this.startCallbackFunction, this);
		startItem.attr({
			x: 102 / 2,
			y: 102 / 2
		});
		this.startItem = startItem;
		var startMenu = new cc.Menu(startItem);
		startMenu.attr({
			x: 490 + 105,
			y: 20
		});
		this.addChild(startMenu)
		//动画
		var startAvi = new ccs.Armature("dh__cdd_kuaisukaishianniu_dh");
		startAvi.getAnimation().playWithIndex(0);
		startAvi.getAnimation().setSpeedScale(1);
		startAvi.setScale(1);
		startAvi.setAnchorPoint(0, 0);
		startAvi.setPosition(-5, -10);
		startItem.addChild(startAvi,3);
		startAvi.getAnimation().setMovementEventCallFunc(this.freeStratCallBackRemove, this);

		//新手场按钮，初级场按钮，中级场按钮，高级场按钮
		var newLevel = new cc.MenuItemSprite(new cc.Sprite(gpNamespace.jdc.Jdc_NewField),
				new tyNamespace.maskSprite(gpNamespace.jdc.Jdc_NewField),
				this.xinshouchangCallbackFunction, this);

		var primaryLevel = new cc.MenuItemSprite(new cc.Sprite(gpNamespace.jdc.Jdc_PrimaryField),new tyNamespace.maskSprite(gpNamespace.jdc.Jdc_PrimaryField),
				this.chujiCallbackFunction, this);

		var middleLevel = new cc.MenuItemSprite(new cc.Sprite(gpNamespace.jdc.Jdc_MiddleField),new tyNamespace.maskSprite(gpNamespace.jdc.Jdc_MiddleField),
				this.zhongjiCallbackFunction, this);

		var advancedLevel = new cc.MenuItemSprite(new cc.Sprite(gpNamespace.jdc.Jdc_AdvancedField),new tyNamespace.maskSprite(gpNamespace.jdc.Jdc_AdvancedField),
				this.gaojiCallbackFunction, this);

		var levelMenu = new cc.Menu(newLevel,primaryLevel,middleLevel,advancedLevel);

		levelMenu.attr({
			x: 89 + 1108 / 2,
			y: 159 + 346 / 2
		});
		levelMenu.alignItemsHorizontally();
		this.levelMenu = levelMenu;
		this.addChild(levelMenu,2);

		var bxLayer = new gpNamespace.BxLayer();
		bxLayer.visible = false;
		this.bxLayer = bxLayer;
		this.addChild(bxLayer);
	},

	startCallbackFunction:function(){
		cc.log("快速开始");
	},

	checkEntrence: function (limitGold) {
		var currentGold = userInfo.gold;
		var pochanCount = gpNamespace.LoginInfo.POCHANCOUNT;
		
		if (parseInt(currentGold) < 100 && parseInt(pochanCount) < 3) {
			tyNamespace.IsPochan = true;
			gpNamespace.GameConfig.SCENETYPE = 0;
			cc.director.runScene(new cc.TransitionFade(0.5, new gpNamespace.GpMainScene()));
			return false;
		}

		var oneRMB = gpNamespace.LoginInfo.ISBUYONE;

		cc.log("pochanCount:" + pochanCount);
		
		if (parseInt(currentGold) < 100 && parseInt(pochanCount) >= 3 && !oneRMB) {
			tyNamespace.IsBuyOne = true;
			gpNamespace.GameConfig.SCENETYPE = 0;
			cc.director.runScene(new cc.TransitionFade(0.5, new gpNamespace.GpMainScene()));
			return false;
		}

		if (!tyNamespace.IsPochan && !gpNamespace.IsBuyOne && parseInt(currentGold) < parseInt(limitGold)) {
			tyNamespace.IsLimit = true;
			gpNamespace.GameConfig.SCENETYPE = 0;
			cc.director.runScene(new cc.TransitionFade(0.5, new gpNamespace.GpMainScene()));
			return false;
		}
		
		return true;
	},

	startCallbackFunction: function (sender) {
		cc.log("开始游戏");
		
		tyNamespace.setMenuEnable(sender);
		
		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);
		
		var self = this;

		tyNamespace.addWaitLayer();
		
		sender.runAction(cc.sequence(cc.scaleTo(0.1, 1.02, 0.98), cc.scaleTo(0.1, 0.98, 1.02), cc.scaleTo(0.1, 1, 1), cc.callFunc(function () {
			if (!self.checkEntrence(300)) {
				return;
			}
			
			//TODO 待优化
			gpNamespace.NetManager.getInstance();
			
			//inRoom (0:麻将，1:斗地主，2:德州,5:跑的快) (1,2,3,4,5,6 房间等级 -1 自动匹配) (0:不自动坐下，1:自动坐下 (包厢为0，其他情况都是1)))
			xinyou.netManager.inRoom(6, -1, 1);
		}, this)));
    },
    
	freeStratCallBackRemove: function (armature, movementType, movementID) {
		if (movementType == ccs.MovementEventType.complete) {
			armature.removeFromParent(true);
		}
	},
	
	jdcCallbackFunction:function(sender){
		console.log("经典场标签点击");
		self = sender.getUserData();
		self.classicField.setSelectedIndex(0);
		self.boxField.setSelectedIndex(1);
		
		self.levelMenu.visible = true; 
		self.startItem.visible = true; 
		self.bxLayer.visible = false;
	},
	bxCallbackFunction:function(sender){
		cc.log("包厢显示");
		self = sender.getUserData();
		self.classicField.setSelectedIndex(1);
		self.boxField.setSelectedIndex(0);
		
		self.levelMenu.visible = false;
		self.startItem.visible = false; 
		self.bxLayer.visible = true;
//		tyNamespace.setMenuEnable(sender);
		/*var runningScene = cc.director.getRunningScene();
		var modelLayerCancelable = new tyNamespace.ModelLayerCancelable();
		var modelLayer = new tyNamespace.ModelLayer(70);
		modelLayer.setTouchArea(cc.size(1138, 576), cc.p(72, 70));
		modelLayerCancelable.addChild(modelLayer);
		runningScene.addChild(modelLayerCancelable, 6);
		var bxLayer = new gpNamespace.BxLayer();
		bxLayer.attr({
			//左上角坐标
			x: 0,
			y: cc.winSize.height
		});
		modelLayer.addChild(bxLayer);
		tyNamespace.AnimationManager.nodeJumpShow(bxLayer, -cc.winSize.height);*/
	},

	xinshouchangCallbackFunction:function(sender) {
		cc.log("新手");

		//tyNamespace.playEffect(tyNamespace.soundRes.ButtonEffect, false);  //音效
		tyNamespace.setMenuEnable(sender);

		var limitGold = gpNamespace.RuchangInfo.RUCHANGDATA[0][1];
		
		tyNamespace.subRoom = 1;

		tyNamespace.addWaitLayer();
		
		if (!this.checkEntrence(limitGold)) {
			return;
		}

		var cddNet = gpNamespace.NetManager.getInstance();
		currentNetManager = cddNet;

		//inRoom (0:麻将，1:斗地主，2:德州 4:锄大地,5:跑的快) (1,2,3,4,5,6 房间等级) (0:不自动坐下，1:自动坐下 (包厢为0，其他情况都是1)))
		//xinyou.netManager.inRoom(1, 1, 1);
		xinyou.netManager.inRoom(6, tyNamespace.subRoom, 1);
	},

	chujiCallbackFunction:function(sender) {
		cc.log("初级");
		//tyNamespace.playEffect(tyNamespace.soundRes.ButtonEffect, false);  //音效
		tyNamespace.setMenuEnable(sender);

		var limitGold = gpNamespace.RuchangInfo.RUCHANGDATA[1][1];
		
		tyNamespace.subRoom = 2;

		tyNamespace.addWaitLayer();

		if (!tyNamespace.checkMoney(limitGold)) {
			return;
		}

		var cddNet = gpNamespace.NetManager.getInstance();
		currentNetManager = cddNet;

		//inRoom (0:麻将，1:斗地主，2:德州 4:锄大地,5:跑的快) (1,2,3,4,5,6 房间等级) (0:不自动坐下，1:自动坐下 (包厢为0，其他情况都是1)))
		//xinyou.netManager.inRoom(1, 1, 1);
		xinyou.netManager.inRoom(6, tyNamespace.subRoom, 1);
	},

	zhongjiCallbackFunction:function(sender) {
		cc.log("中级");

		//tyNamespace.playEffect(tyNamespace.soundRes.ButtonEffect, false);  //音效
		tyNamespace.setMenuEnable(sender);

		var limitGold = gpNamespace.RuchangInfo.RUCHANGDATA[2][1];
		
		tyNamespace.subRoom = 3;

		tyNamespace.addWaitLayer();

		if (!tyNamespace.checkMoney(limitGold)) {
			return;
		}

		var cddNet = gpNamespace.NetManager.getInstance();
		currentNetManager = cddNet;

		//inRoom (0:麻将，1:斗地主，2:德州 4:锄大地,5:跑的快) (1,2,3,4,5,6 房间等级) (0:不自动坐下，1:自动坐下 (包厢为0，其他情况都是1)))
		//xinyou.netManager.inRoom(1, 1, 1);
		xinyou.netManager.inRoom(6, tyNamespace.subRoom, 1);
	},

	gaojiCallbackFunction:function(sender) {
		cc.log("高级");
		//tyNamespace.playEffect(tyNamespace.soundRes.ButtonEffect, false);  //音效
		tyNamespace.setMenuEnable(sender);

		var limitGold = gpNamespace.RuchangInfo.RUCHANGDATA[3][1];
		
		tyNamespace.subRoom = 4;

		tyNamespace.addWaitLayer();

		if (!tyNamespace.checkMoney(limitGold)) {
			return;
		}

		var cddNet = gpNamespace.NetManager.getInstance();
		currentNetManager = cddNet;

		//inRoom (0:麻将，1:斗地主，2:德州 4:锄大地,5:跑的快) (1,2,3,4,5,6 房间等级) (0:不自动坐下，1:自动坐下 (包厢为0，其他情况都是1)))
		//xinyou.netManager.inRoom(1, 1, 1);
		xinyou.netManager.inRoom(6, tyNamespace.subRoom, 1);
	},

	settingCallbackFunction:function() {
		cc.log("设置2");
	},

	addJuhua:function(){
		var runningScene = cc.director.getRunningScene();
		var waitingLayer = new tyNamespace.WaittingLayer(100,cc.p(cc.winSize.width / 2,cc.winSize.height / 2));
		waitingLayer.setName(tyNamespace.Juhua);
		runningScene.addChild(waitingLayer, 5);//todo
	},

	setDizhu: function (text, position) {
		var label = new cc.LabelTTF("底注：" + text.toString(), gpNamespace.DEFAULTFONT, 25);
		label.attr({
			x: position.x,
			y: position.y
		});
		this.addChild(label);
	},

	setInfo: function (text, position) {
		var label = new cc.LabelTTF(text + "可进场", "Arial", 25);
		label.setFontFillColor(cc.color(0, 31, 84, 255));
		label.attr({
			x: position.x,
			y: position.y
		});
		this.addChild(label);
	},

	setPeople: function (text, position) {
		var label = new cc.LabelTTF(text.toString() + "人在线", "Arial", 25);
		label.setFontFillColor(cc.color(255, 228, 0, 255));
		label.attr({
			x: position.x,
			y: position.y
		});
		this.addChild(label);
	},
	
	setRuchangInfo: function () {
		//cc.log(gpNamespace.RuchangInfo.ITEMCOUNT);
		//for (var a = 0; a < gpNamespace.RuchangInfo.ITEMCOUNT; a++) {
		//    cc.log(gpNamespace.RuchangInfo.RUCHANGDATA[a][0]);
		//    cc.log(gpNamespace.RuchangInfo.RUCHANGDATA[a][1]);
		//    cc.log(gpNamespace.RuchangInfo.RUCHANGDATA[a][2]);
		//}

		for (var a = 0; a < gpNamespace.RuchangInfo.ITEMCOUNT; a++) {
			this.setDizhu(gpNamespace.RuchangInfo.RUCHANGDATA[a][0], this.dizhuPosition[a]);
			this.setInfo(gpNamespace.RuchangInfo.RUCHANGDATA[a][1], this.infoPosition[a]);
			this.setPeople(gpNamespace.RuchangInfo.RUCHANGDATA[a][2], this.peoplePosition[a]);
		}
	}

});

